package com.example.studyktdemo.base

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import java.lang.reflect.ParameterizedType

abstract class BaseFragment<T : ViewDataBinding> : Fragment() {

    lateinit var binding: T
    //lateinit var viewModel: M

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
       /* viewModel =
            ViewModelProvider(this, ViewModelProvider.NewInstanceFactory())[viewModel::class.java]*/
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        binding = DataBindingUtil.inflate(inflater, bindLayout(), container, false)

        return binding.root
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)

        initView()
        initData()
        initObserver()
    }

    //返回视图
    protected abstract fun bindLayout(): Int

    //初始化界面
    protected abstract fun initView()

    //初始化数据
    protected abstract fun initData()

    //初始化观察者
    protected abstract fun initObserver()

    //初始化事件监听
    protected abstract fun initListener()
}
